$import( "SmitearWebGL.Objects.Parser.Collada.DaeEntity" );
$import( "SmitearWebGL.Objects.Parser.Collada.DaeChannel" );
$import( "SmitearWebGL.Objects.Parser.Collada.DaeSampler" );

SmitearWebGL.Objects.Parser.Collada.DaeAnimation = SmitearWebGL.Objects.Parser.Collada.DaeEntity.extend(
{
	/******** constructor ********/

	init : function( document, node )
	{
		this.channels = null;
		this.animations = null;
		this._newID = 0;

		this._super( document, node );
		this._registClass( "SmitearWebGL.Objects.Parser.Collada.DaeAnimation" );
	}
	,
	read : function( node )
	{
		$THROW( node.localName == SmitearWebGL.Objects.Parser.Collada.Const.DAE_ANIMATION_ELEMENT,
		"expected a '" + SmitearWebGL.Objects.Parser.Collada.Const.DAE_ANIMATION_ELEMENT + "' element" );

		this.animations = [];
		this.channels = [];
		var samplers = [];

		this._super( node );

		this.id = (this.id && this.id.length) ? this.id : "animation_" + (_newID++);

		node.setAttribute( "id", this.id );

		var list = node.childNodes;
		var child;
		var num = list.length;
		var i;

		for( i = 0; i < num; i ++ )
		{
			child = list[i];

			switch ( child.localName )
			{
				case "animation":
					this.animations.push(new SmitearWebGL.Objects.Parser.Collada.DaeAnimation(this.document, child));
					break;

				case "channel":
					this.channels.push(new SmitearWebGL.Objects.Parser.Collada.DaeChannel(this.document, child));
					break;

				case "sampler":
					samplers.push(new SmitearWebGL.Objects.Parser.Collada.DaeSampler(this.document, child));
					break;

				default:
					break;
			}
		}

		if( this.channels.length && this.channels.length == samplers.length )
		{
			var tmp = [];
			for( i = 0; i < this.channels.length; i ++ )
			{
				var channel = this.channels[i];

				channel.sampler = samplers[i];

				if(channel.sampler.input && channel.sampler.input.values &&
				channel.sampler.input.values.length)
				{
					tmp.push(channel);

					var animID = channel.syntax.targetID;

					this.document.animatables[animID] = this.document.animatables[animID] || [];
					this.document.animatables[animID].push(channel);
				}
			}
			this.channels = tmp;
		}
	}
}
);
